import React from 'react';
import {Table, Button, Modal, message} from 'antd';
import {connect} from 'react-redux';
import NoteListView from './noteListView';
import {r} from "../../login/request";

const confirm = Modal.confirm;

class NoteList extends React.Component{

    constructor(props) {
        super(props);
        this.state = {
            totalPages: 1,
            noteList: [{noteId: 1, title: 'cnm', createTime: '2017年1月20日', updateTime: '2017年3月22日'}],
            totalElements: 0
        };

        this.onNoteEdit = this.onNoteEdit.bind(this);
        this.onNoteDelete = this.onNoteDelete.bind(this);
    }

    fetchNoteList(page) {
        if(!page) page = this.props.match.params.page;
        r.fetchNoteList({page})
            .then(({data}) => {
            const {totalPages, totalElements, content} = data;
            this.setState({totalPages, totalElements, noteList: content})
        })
    }

    onNoteEdit(noteId) {
        this.props.history.push(`/edit-note/${noteId}?c=${this.props.location.pathname}`)
    }

    onNoteDelete(noteId){
        confirm({
            title: '确认删除笔记',
            content: '笔记删除后将无法恢复，确认删除吗？',
            okText: '删除',
            okType: 'danger',
            cancelText: '取消',
            onOk(){

            },
            onCancel(){

            }
        });
    }

    render(){
        const listProps = {
            onNoteEdit: this.onNoteEdit,
            onNoteDelete: this.onNoteDelete
        };
        return(
            <NoteListView {...listProps} noteList={this.state.noteList} onNoteEdit={this.onNoteEdit} onNoteDelete={this.onNoteDelete}/>
        )
    }
}

const mapStateToProps = ({auth}) => ({auth});

export default connect(mapStateToProps)(NoteList)